package com.shanghaionstar.home.life;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;

import com.shanghaionstar.R;
import com.shanghaionstar.utils.FinalConstants;

import java.util.Set;


/**
 * Created by Rengui Lu on 2017/1/6.
 */
public class AlarmReceiver extends BroadcastReceiver {
//    private NotificationManager manager;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(AlarmManagerUtil.IAlarmAction.ALARM_RECEIVER_RENEWAL_ACTION)) {
            notifyRenewal(context, intent);
        } else if (action.equals(AlarmManagerUtil.IAlarmAction.ALARM_RECEIVER_COMPULSORY_ASSURANCE_ACTION)
                || action.equals(AlarmManagerUtil.IAlarmAction.ALARM_RECEIVER_BUSINESS_ASSURANCE_ACTION)) {
            boolean isCompulsory = action.equals(AlarmManagerUtil.IAlarmAction.ALARM_RECEIVER_COMPULSORY_ASSURANCE_ACTION);
            String content = isCompulsory ?
                    context.getResources().getString(R.string.reminder_driver_compulsory_assurance) :
                    context.getResources().getString(R.string.reminder_driver_business_assurance);
            notifyInsurance(context, intent, content, isCompulsory);
        }
    }

    private void notifyRenewal(Context context, Intent intent) {
        String className = RenewalFragment.class.getName();
        Set<String> categories = intent.getCategories();
        String content = context.getResources().getString(R.string.reminder_driver_renewal);
        if (categories.contains(AlarmManagerUtil.IRenewalCategory.ALARM_RECEIVER_CATEGORY_DAYS60)) {
            String mContent = String.format(content, "60");
            showNotification(context, mContent, className, 60);
        } else if (categories.contains(AlarmManagerUtil.IRenewalCategory.ALARM_RECEIVER_CATEGORY_DAYS30)) {
            String mContent = String.format(content, "30");
            showNotification(context, mContent, className, 30);
        } else if (categories.contains(AlarmManagerUtil.IRenewalCategory.ALARM_RECEIVER_CATEGORY_DAYS7)) {
            String mContent = String.format(content, "7");
            showNotification(context, mContent, className, 7);
        } else {
            String todayContent = context.getResources().getString(R.string.reminder_driver_renewal_today);
            showNotification(context, todayContent, className, 1);
        }
    }

    private void notifyInsurance(Context context, Intent intent, String content, boolean isCompulsory) {
        String className = AssuranceFragment.class.getName();
        Set<String> categories = intent.getCategories();

        if (categories.contains(AlarmManagerUtil.IAssuranceCategory.ALARM_RECEIVER_CATEGORY_DAYS90)) {
            String mContent = String.format(content, "90");
            showNotification(context, mContent, className, isCompulsory ? 91 : 92);
        } else if (categories.contains(AlarmManagerUtil.IAssuranceCategory.ALARM_RECEIVER_CATEGORY_DAYS60)) {
            String mContent = String.format(content, "60");
            showNotification(context, mContent, className, isCompulsory ? 61 : 62);
        } else if (categories.contains(AlarmManagerUtil.IAssuranceCategory.ALARM_RECEIVER_CATEGORY_DAYS30)) {
            String mContent = String.format(content, "30");
            showNotification(context, mContent, className, isCompulsory ? 31 : 32);
        }
    }


    private void showNotification(Context context, String content, String className, int id) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent playIntent = new Intent(context, OwnerLifeActivity.class);
        playIntent.putExtra(FinalConstants.JUMP_FLAG, className);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, id, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(context.getString(R.string.app_name)).setContentText(content)
                .setSmallIcon(R.drawable.logo_circle12_new)
                .setLargeIcon(BitmapFactory.decodeResource(
                        context.getResources(), R.drawable.logo_circle12_new))
                .setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true);
        manager.notify(id, builder.build());
    }

}
